/**
 * author:Administrator
 * date:2016/3/14
 * desc: 整体 command 流程
 */

'use strict';
let co = require('co');
//
let roleService = require('./application/roleService.js');
let customerService = require('./application/customerCmdService.js');
let managerService = require('./application/managerCmdService.js');
let vendorService = require('./application/vendorCmdService.js');
let keeperService = require('./application/keeperCmdService.js');
let queryService = require('./application/queryService.js');

// int [0,max)
function rdm(max) {
  return parseInt(Math.random() * max);
}

/******************************************************
 *                    添加用户                         *
 ******************************************************/
let users = {
  manager: {name: 'root', pwd: 'root'},
  vendor: {name: 'vendor1', pwd: 'vendor1'},
  customer: {name: 'customer1', pwd: 'customer1'}
};

function* addUser() {
  yield roleService.registerManager(users.manager.name, users.manager.pwd);
  yield roleService.registerVendor(users.vendor.name, users.vendor.pwd);
  yield roleService.registerCustomer(users.customer.name, users.customer.pwd);
  console.log(' [x] addUser success');
}

/******************************************************
 *                    添加位置                         *
 ******************************************************/
// 荔湾区、海珠区、黄埔区、花都区、萝岗区、番禺区、南沙区
let locations = [
  {
    name: '广州',
    child: [
      {name: '天河', child: [{name: '岑村'}, {name: '车陂'}, {name: '程介村'}]},
      {name: '越秀', child: [{name: '百灵路'}, {name: '北京路'}]},
      {name: '白云'}]
  }
];

let locations_result = [];
function* addLocation() {
  let list = [].concat(locations);

  while (list.length > 0) {
    let loc = list.pop();
    console.log('save loc', loc.name, loc.pid);
    let pLoc = yield managerService.createLocation(loc.name, loc.pid);
    locations_result.push(pLoc);

    if (loc.child && loc.child.length > 0) {
      for (let c of loc.child) {
        c.pid = pLoc.id;
        list.push(c);
      }
    }
  }
  //for (let i = 0; i < locations.length; i++) {
  //  let loc = locations[i];
  //
  //  let pLoc = yield managerService.createLocation(loc.name);
  //  console.log(pLoc);
  //  locations_result.push(pLoc);
  //
  //  let child = loc.child;
  //  for (let j = 0; j < child.length; j++) {
  //    loc = child[j];
  //    let aChild = yield managerService.createLocation(loc.name, pLoc.id);
  //    console.log(aChild);
  //    locations_result.push(aChild);
  //  }
  //}
  console.log(' [x] addLocation success');
}
function rdmLoc() {
  let rdmIndex = rdm(locations_result.length);
  let loc = locations_result[rdmIndex];
  //console.log('rdmLoc', loc);
  return loc;
}

/******************************************************
 *                    添加类别                         *
 ******************************************************/
let categories = [
  {name: '护肤'},
  {name: '祛痘'},
  {name: '美白'},
];
let categories_result = [];
function* addCategory() {
  for (let i = 0; i < categories.length; i++) {
    let cate = categories[i];
    let result = yield managerService.createCategory(cate.name, null);
    console.log(result);
    categories_result.push(result);
  }
  console.log(' [x] addCategory success');
}
function rdmCate() {
  let rdmIndex = rdm(categories_result.length);
  return categories_result[rdmIndex];
}

/******************************************************
 *                    添加商铺                         *
 ******************************************************/
let shops = [
  {name: '商铺1'},
  {name: '商铺2'},
  {name: '商铺3'},
  {name: '商铺4'},
  //{name: '商铺5'},
  //{name: '商铺6'},
];
let shops_result = [];
function* addShop() {
  let vd = yield roleService.login(users.vendor.name, users.vendor.pwd);
  for (let i = 0; i < shops.length; i++) {
    let shop = shops[i];
    let loc = rdmLoc();
    let result = yield vendorService.createShop(vd.id, shop.name, loc.id);
    console.log(result);
    shops_result.push(result);
  }
  console.log(' [x] addShop success');
}


/******************************************************
 *                    添加商品                         *
 ******************************************************/
let wares = [
  {name: '商品1', quantity: 100, price: 1.99},
  {name: '商品2', quantity: 200, price: 2.99},
  {name: '商品3', quantity: 300, price: 3.99},
  {name: '商品4', quantity: 400, price: 4.99},
  //{name: '商品5', quantity: 500, price: 5},
  //{name: '商品6', quantity: 600, price: 6},
];
let wares_result = [];
function* addWare() {
  let vd = yield roleService.login(users.vendor.name, users.vendor.pwd);
  for (let i = 0; i < wares.length; i++) {
    let ware = wares[i];
    let cate = rdmCate();
    let result = yield vendorService.createWare(vd.id, ware.name, cate.id, ware.quantity, ware.price);
    wares_result.push(result);
  }
  console.log(' [x] addWare success');
}

/******************************************************
 *                    添加商品商铺关联                   *
 ******************************************************/
function* addWareToShop() {
  let vd = yield roleService.login(users.vendor.name, users.vendor.pwd);
  let shopIds = shops_result.map(item =>item.id);
  for (let i = 0; i < wares_result.length; i++) {
    let ware = wares_result[i];
    let result = yield vendorService.wareOnShelf(vd.id, ware.id, shopIds);
    console.log(result);
  }
  console.log(' [x] addWareToShop success');
}


/******************************************************
 *                    添加优惠券                        *
 ******************************************************/
let coupons = [
  {maxCount: 1, money: 10, consumeLimit: 100},
  {maxCount: 10, money: 3, consumeLimit: 20},
  {maxCount: -1, money: 1, consumeLimit: 10},
];
function* addCoupon() {
  let vd = yield roleService.login(users.vendor.name, users.vendor.pwd);
  for (let i = 0; i < coupons.length; i++) {
    let coupon = coupons[i];
    yield vendorService.createCouponMjs(vd.id, null, null, coupon.maxCount, coupon.consumeLimit, coupon.money, null);
  }
  console.log(' [x] addCoupon success');
}

co(function*() {
  yield addUser();
  yield addLocation();
  yield addCategory();
  yield addShop();
  yield addWare();
  yield addWareToShop();
  yield addCoupon();

  console.log(' [x] ****** all done ******')
}).catch(function (err) {
  console.log(err.stack);
});
